package com.axxc.jpa.web;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;

import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: RedisTestController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author xxc
 * @date 2018年8月17日 上午9:44:45
 * @see {@link https://gitee.com/biuabiu}
 */
@RestController
@RequestMapping("redis")
public class RedisTestController extends BaseController {

	// @GetMapping("testAddSet")
	public void testSet() {
		for (int i = 0; i < 5; i++) {
			super.stringRedisTemplate.opsForSet().add("-99", UUID.randomUUID().toString());
		}
	}

	// https://blog.csdn.net/lichuangcsdn/article/details/80866182
	// @GetMapping("testGetSet")
	public void testGetSet() {
		super.stringRedisTemplate.opsForSet().members("-99").forEach(s -> logger.info(s));
	}

	@GetMapping("testPutMap")
	public Object testPutMap(@RequestParam(name = "key") String key) {
		for (int i = 0; i < 5; i++) {
			super.stringRedisTemplate.opsForHash().put(key, getUUIDValue(), "-99");
		}
		return testGetMap(key, null);
	}

	private String getUUIDValue() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

	@GetMapping("testDeleteMapValueByKey")
	public void testDeleteMap(@RequestParam(name = "key") String key) {
		Map<Object, Object> map = super.stringRedisTemplate.opsForHash().entries(key);
		for (Entry<Object, Object> entry : map.entrySet()) {
			super.stringRedisTemplate.opsForHash().delete(key, entry.getKey().toString());
		}
	}

	// https://blog.csdn.net/lichuangcsdn/article/details/80866182
	@GetMapping("testGetMap")
	public Object testGetMap(@RequestParam(name = "key") String key,
			@RequestParam(name = "bucketName", required = false) String bucketName) {
		Map<Object, Object> map = super.stringRedisTemplate.opsForHash().entries(key);
		return map;
	}

	@GetMapping("testListMapValues")
	public List<Object> testListMap(@RequestParam(name = "key") String key) {
		return super.stringRedisTemplate.opsForHash().values(key);
	}

	@GetMapping("testHyperLogLogAdd")
	public void testHyperLogLogAdd(@RequestParam(name = "key") String key) {

		for (int i = 0; i < 100000; i++) {
			super.stringRedisTemplate.opsForHyperLogLog().add(key, i + "");
		}

	}

	@GetMapping("testHyperLogLogSize")
	public Object testHyperLogLogSize(@RequestParam(name = "key") String key) {
		Long add = super.stringRedisTemplate.opsForHyperLogLog().size(key);
		return add;
	}

	@GetMapping("testGeoAdd")
	public void testGeoAdd(@RequestParam(name = "key") String key, @RequestParam(name = "gpsPoint") String gpsPoint,
			@RequestParam(name = "cityName") String cityName) {
		String[] split = gpsPoint.split(",");
		Point point = new Point(Double.valueOf(split[1]), Double.valueOf(split[0]));
		super.stringRedisTemplate.opsForGeo().geoAdd(key, point, cityName);
	}

	@GetMapping("testGeoComputDis")
	public void testGeoComputDis(@RequestParam(name = "key") String key,
			@RequestParam(name = "cityName1") String cityName1, @RequestParam(name = "cityName2") String cityName2) {
		Distance distance = super.stringRedisTemplate.opsForGeo().geoDist(key, cityName1, cityName2,
				Metrics.KILOMETERS);
		System.out.println(distance.getValue() + " " + distance.getUnit().toUpperCase());
		// 获取所有元素的坐标
		List<Point> geoPos = super.stringRedisTemplate.opsForGeo().geoPos(key, cityName1, cityName2);
		for (Point point : geoPos) {
			System.out.println(point);
		}
		// 获取所有元素坐标的Hash值
		List<String> geoHash = super.stringRedisTemplate.opsForGeo().geoHash(key, cityName1, cityName2);
		for (String hash : geoHash) {
			// http://geohash.org/${hash}
			System.out.println(hash);
		}
		// 指定范围
		Distance distanceR = new Distance(1600, Metrics.KILOMETERS);
		GeoRadiusCommandArgs aaa = GeoRadiusCommandArgs.newGeoRadiusArgs();
		// aaa.sortDescending();// 倒序
		GeoResults<GeoLocation<String>> geoResults = super.stringRedisTemplate.opsForGeo().geoRadiusByMember(key,
				cityName1, distanceR, aaa);
		System.out.println(cityName1);
		for (GeoResult<GeoLocation<String>> geoResult : geoResults) {
			System.out.println(geoResult);
		}

	}

}
